package com.tsp_tux.demo.Util;

import lombok.SneakyThrows;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.InputStreamReader;
import java.util.Objects;

/**
 * @author 刘海生
 * @version v1.0
 */
public class Des {

    @SneakyThrows
    public String strEnc(String data, String firstKey, String secondKey, String thirdKey){
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("js");

        // FileReader的参数为所要执行的js文件的路径
        scriptEngine.eval(new InputStreamReader(Objects.requireNonNull(Des.class.getResourceAsStream("/des.js"))));
        Invocable invocable = (Invocable) scriptEngine;
        Methods EncMethod = invocable.getInterface(Methods.class);
        return EncMethod.strEnc(data, firstKey, secondKey, thirdKey);
    }

    public interface Methods {
        String strEnc(String data, String firstKey, String secondKey, String thirdKey);
    }
}
